﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using CONEK911Manager.DTO;

namespace CONEK911Manager.DAO
{
    class OrderDetailDAO
    {
        readonly string _connectionString = ConfigurationManager.ConnectionStrings["Conek911ConnectionString"].ConnectionString;
        public bool AddOrderDetail(OrderDetailDTO detail)
        {
            try
            {
                using (var con = new SqlConnection(_connectionString))
                {
                    const string sql = "INSERT into [Account] (OrderID, ProductID, Quantity) VALUES (@orderId, @productId, @quantity)";
                    using (var sqlAddDetail = new SqlCommand(sql))
                    {
                        sqlAddDetail.Connection = con;
                        sqlAddDetail.Parameters.Add(new SqlParameter("@orderId", detail.OrderID));
                        sqlAddDetail.Parameters.Add(new SqlParameter("@productId", detail.Code));
                        sqlAddDetail.Parameters.Add(new SqlParameter("@quantity", detail.Quantity));
                        con.Open();
                        var result = sqlAddDetail.ExecuteNonQuery();
                        con.Close();
                        if (result == 1)
                        {
                            return true;
                        }
                        return false;
                    }
                }
            } catch (Exception e)
            {
                try
                {
                    Logger.WriteLog(e.Message + "-" + e.StackTrace);
                }
                catch
                { }
                return false;
            }
        }

        public List<OrderDetailDTO> GetAllOrderDetail()
        {
            var listDetail = new List<OrderDetailDTO>();
            using (var con = new SqlConnection(_connectionString))
            {
                const string sql = "SELECT * FROM [OrderDetail]";
                using (var cmd = new SqlCommand(sql))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var detail = new OrderDetailDTO
                                              {
                                                  OrderDetailId = int.Parse(Convert.ToString(reader["Account"])),
                                                  OrderID = int.Parse(Convert.ToString(reader["OderID"])),
                                                  Code = Convert.ToString(reader["Code"]),
                                                  Quantity = int.Parse(Convert.ToString(reader["Quantity"])),
                                              };
                            listDetail.Add(detail);
                        }
                    }
                    con.Close();
                }
            }

            return listDetail;
        }
    }
}
